﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DotNetOpenAuth.OpenId;
using DotNetOpenAuth.OpenId.RelyingParty;
using DotNetOpenAuth.OpenId.Extensions.SimpleRegistration;
using DotNetOpenAuth.OpenId.Extensions.AttributeExchange;

public partial class LoginPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        OpenIdRelyingParty rp = new OpenIdRelyingParty();
        var response = rp.GetResponse();
        if (response != null)
        {
            switch (response.Status)
            {
                case AuthenticationStatus.Authenticated:

                    ClaimsResponse sregResponse = response.GetExtension<ClaimsResponse>();
                    FetchResponse axResponse = response.GetExtension<FetchResponse>();
                    string nick = response.FriendlyIdentifierForDisplay;

                    string homepage = response.ClaimedIdentifier;
                    string email = "none@none.com";

                    if (sregResponse != null)
                    {
                        nick = string.IsNullOrEmpty(sregResponse.Nickname) ? sregResponse.FullName : sregResponse.Nickname;
                        email = sregResponse.Email;
                    }

                    if (String.IsNullOrEmpty(nick) && axResponse != null && axResponse.Attributes.Contains(WellKnownAttributes.Name.First) && axResponse.Attributes.Contains(WellKnownAttributes.Name.Last))
                    {
                        nick = axResponse.GetAttributeValue(WellKnownAttributes.Name.First) + " " + axResponse.GetAttributeValue(WellKnownAttributes.Name.Last);
                    }


                    Session["GoogleIdentifier"] = response.ClaimedIdentifier.ToString();
                    Session["hello"] = "hello";
                    Response.Redirect("Default.aspx?nick="+nick+"&email="+email);
                    break;
                case AuthenticationStatus.Canceled:
                    Session["GoogleIdentifier"] = "Cancelled.";
                    break;
                case AuthenticationStatus.Failed:
                    Session["GoogleIdentifier"] = "Login Failed.";
                    break;
            }
        }
        else
        {
            if (!Page.IsPostBack)
            {
                string discoveryUri = "https://www.google.com/accounts/o8/id";
                OpenIdRelyingParty openid = new OpenIdRelyingParty();
                var URIbuilder = new UriBuilder(Request.Url) { Query = "" };
                IAuthenticationRequest req = openid.CreateRequest(discoveryUri, URIbuilder.Uri, URIbuilder.Uri);

                ClaimsRequest sregRequest = new ClaimsRequest();
                sregRequest.Email = DemandLevel.Require;
                sregRequest.Nickname = DemandLevel.Request;
                sregRequest.FullName = DemandLevel.Request;
                sregRequest.Country = DemandLevel.Request;
                sregRequest.Gender = DemandLevel.Request;
                sregRequest.BirthDate = DemandLevel.Request;
                req.AddExtension(sregRequest);

                FetchRequest axRequest = new FetchRequest();
                axRequest.Attributes.AddRequired(WellKnownAttributes.Name.First);
                axRequest.Attributes.AddRequired(WellKnownAttributes.Name.Last);
                axRequest.Attributes.AddRequired(WellKnownAttributes.BirthDate.WholeBirthDate);
                axRequest.Attributes.AddRequired(WellKnownAttributes.Person.Gender);
                axRequest.Attributes.AddRequired(WellKnownAttributes.Preferences.Language);
                axRequest.Attributes.AddRequired(WellKnownAttributes.Company.CompanyName);
                axRequest.Attributes.AddRequired(WellKnownAttributes.Company.JobTitle);
                axRequest.Attributes.AddRequired(WellKnownAttributes.Contact.Phone.Mobile);
                req.AddExtension(axRequest);



                req.RedirectToProvider();
            }
        }

    }

    protected void btnGoogleLogin_Click(object sender, CommandEventArgs e)
    {
        //string discoveryUri = e.CommandArgument.ToString();
        //OpenIdRelyingParty openid = new OpenIdRelyingParty();
        //var URIbuilder = new UriBuilder(Request.Url) { Query = "" };
        //var req = openid.CreateRequest(discoveryUri, URIbuilder.Uri, URIbuilder.Uri);
        //req.RedirectToProvider();
    }
}